<?php
namespace common\forms\login;

use Yii;

/**
 * 登陆表单
 */
class LoginForm extends BaseLoginForm
{
    public $username;
    public $password;
    public $mobile;
    public $email;
    public $code;

    private $_accountType;

    public $autoSignup = false;

    private $_userIdentity;

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'username'=>'用户名',
            'mobile'=>'手机号码',
            'email'=>'邮箱号码',

            'password'=>'密码',
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username','password'], 'trim', 'on'=>self::SCENARIO_DEFAULT],
            [['username','password'], 'required', 'on'=>self::SCENARIO_DEFAULT],
            ['username', 'string', 'min'=>2, 'on'=>self::SCENARIO_DEFAULT],
            ['password', 'string', 'min'=>5, 'on'=>self::SCENARIO_DEFAULT],
            ['username', 'validateAccount', 'on'=>self::SCENARIO_DEFAULT, 'message' => '账号或密码错误!'],
            ['password', 'validatePassword', 'on'=>self::SCENARIO_DEFAULT],
            

            ['mobile', 'trim', 'on'=>'smscode'],
            ['mobile', 'required', 'on'=>'smscode'],
            ['mobile', 'integer', 'on'=>'smscode'],
            ['mobile', 'string', 'length' => 11, 'on'=>'smscode'],
            ['mobile', 'validateAccount', 'on'=>'smscode', 'message' => '账号找不到!'],

            ['email', 'trim', 'on'=>'emailcode'],
            ['email', 'required', 'on'=>'emailcode'],
            ['email', 'email', 'on'=>'emailcode'],
            ['email', 'validateAccount', 'on'=>'emailcode', 'message' => '账号找不到!'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function setAttributes($values, $safeOnly = true)
    {
        if(isset($values['verify_type'])){
            $this->setScenario($values['verify_type']);
        }

        parent::setAttributes($values, $safeOnly = true);

        if(!$this->mobile && isset($values['username'])){
            $this->mobile = $values['username'];

        }

        if(!$this->email && isset($values['username'])){
            $this->email = $values['username'];
        }
    }

    /**
     * 密码验证
     */
    public function validatePassword($attribute, $params, $inlineValidator)
    {
        $user = $this->getUserIdentity();
        if(!$user || !$user->validatePassword($this->$attribute)){
            $this->addError($attribute, $inlineValidator->message?:'账号或密码错误！');
            return false;
        }
        return true;
    }

    /**
     * 用户验证
     */
    public function validateAccount($attribute, $params, $inlineValidator)
    {
        if($attribute != 'username'){
            $this->username = $this->$attribute;
            $this->_accountType = $attribute;
        }
        if($this->getUserIdentity()){
            return true;
        }
        $this->addError($attribute, $inlineValidator->message?:'账号找不到！');
        return false;
    }

    /**
     * 验证码验证
     */
    public function validateCode($attribute, $params, $inlineValidator)
    {
        // $value = $this->$attribute;
        // $this->addError($attribute, $inlineValidator->message?:'验证码错误！');
        //TODO 短信验证
        $this->addError($attribute, '目前不支持验证码登录！');
        return false;
    }

    /**
     * 获取用户
     */
    public function getUserIdentity(){
        if($this->_userIdentity || $this->_userIdentity === false){
            return $this->_userIdentity?:null;
        }
        $userClassName = Yii::$app->getUser()->identityClass;
        $this->_userIdentity = ($userClassName::findIdentityByAccountNumber($this->username, $this->_accountType)?:false);     
        return $this->_userIdentity?:null;
    }
}